package com.xxx.yuedong.community.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xxx.yuedong.common.R;
import com.xxx.yuedong.common.consts.ActionConstants;
import com.xxx.yuedong.community.controller.form.FollowForm;
import com.xxx.yuedong.community.pojo.Follow;
import com.xxx.yuedong.community.service.FollowService;
import com.xxx.yuedong.utils.EventMsgSendUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 关注功能web层
 * 基本版
 * 整体架构：
 *   web层(controller)->业务层(service)->dao层(MySQL)
 * @Author YinXi
 * @Versin 1.0.0
 * @Date 2023/8/3
 */
@Slf4j
@RestController
@Api(tags = "关注")
@RequestMapping("/follow")
public class FollowController {
    @Autowired
    private FollowService followService;
    public FollowController(){
        log.debug("【FollowController】关注功能web层对象创建");
    }
    /**
     * 关注功能
     * 业务架构：web层(controller)->业务层(service)->dao层(MySQL)
     */
    @PostMapping("/get-follow/{toId:[0-9]+}")
    @ApiOperation("关注")
    @ApiOperationSupport(order = 100)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "toId", value = "被关注者id", required = true, dataType = "long")
    })
//    @SaCheckPermission(value = "community/follow")
    public R getFollow(@PathVariable @NotNull @Range(min=1,message = "请提交有效的to_id") Long toId){
        log.debug("调用接口【FollowController>>>getFollow】实现【关注】获取到前端传递的被关注者id{}",toId);
//        获取关注者id
        Long id = Long.parseLong((String) StpUtil.getLoginId());
        FollowForm form = new FollowForm();
//        Long id = 2L;
        form.setFromId(id);
        form.setToId(toId);
        String key = followService.getFollow(form);

//        发送消息
        EventMsgSendUtil.sendEventMsg(id, toId, ActionConstants.FOLLOWED);
        R r = new R();
        r.put("result",key);
        return r;
    }
    /**
     * 取消关注功能
     * 业务架构：web层(controller)->业务层(service)->dao层(MySQL)
     */
    @PostMapping("/out-follow/{toId:[0-9]+}")
    @ApiOperation("取消关注")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "toId", value = "被关注者id", required = true, dataType = "long")
    })
//    @SaCheckPermission(value = "community/follow")
    public R outFollow(@PathVariable @NotNull @Range(min=1,message = "请提交有效的to_id") Long toId){
        log.debug("调用接口【FollowController>>>outFollow】实现【取消关注】获取到前端传递的被关注者id{}",toId);
//        获取关注者id
        Long id = Long.parseLong((String) StpUtil.getLoginId());
        FollowForm form = new FollowForm();
//        Long id = 2L;
        form.setFromId(id);
        form.setToId(toId);
        followService.outFollow(form);
        return R.ok();
    }
    /**
     * 获取当前账号的关注者数量
     * 业务架构：web层(controller)->业务层(service)->dao层(MySQL)
     */
    @GetMapping("/count-follow")
    @ApiOperation("获取关注者数量")
    @ApiOperationSupport(order = 400)
//    @SaCheckPermission(value = "community/follow")
    public R countFollow(){
        log.debug("调用接口【FollowController>>>countFollow】实现【获取关注者数量】");
//        获取关注者id
        Long id = Long.parseLong((String) StpUtil.getLoginId());
//        Long id = 2L;
        int num = followService.countFollow(id);
        R r = new R();
        r.put("num",num);
        return r;
    }
    /**
     * 获取当前账号的粉丝数量
     * 业务架构：web层(controller)->业务层(service)->dao层(MySQL)
     */
    @GetMapping("/count-tofollow")
    @ApiOperation("获取粉丝数量")
    @ApiOperationSupport(order = 410)
//    @SaCheckPermission(value = "community/follow")
    public R countToFollow(){
        log.debug("调用接口【FollowController>>>countToFollow】实现【获取粉丝数量】");
//        获取关注者id
        Long id = Long.parseLong((String) StpUtil.getLoginId());
//        Long id = 2L;
        int num = followService.countToFollow(id);
        R r = new R();
        r.put("num",num);
        return r;
    }
    /**
     * 判断关注者是否关注过被关注者
     */
    @GetMapping("/is-foller/{toId:[0-9]+}")
    @ApiOperationSupport(order = 400)
    @ApiOperation("当前用户是否关注过被关注者")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "toId", value = "被关注者id", required = true, dataType = "long")
    })
    public R isFollower(@PathVariable @NotNull @Range(min=1,message = "请提交有效的to_id") Long toId){
//        获取关注者id
        Long id = Long.parseLong((String) StpUtil.getLoginId());
        FollowForm form = new FollowForm();
        form.setFromId(id);
        form.setToId(toId);
        List<FollowForm> follows = followService.isFollower(form);
        if(follows.isEmpty()){
            return R.ok().put("isFollower","false");
        }
        return R.ok().put("isFollower","true");
    }
}
